package com.zkq.graduate.conf;

import com.zkq.graduate.interceptor.AuthInterceptor;
import com.zkq.graduate.interceptor.LoginInterceptor;
import com.zkq.graduate.interceptor.SendMessageInterceptor;
import com.zkq.graduate.interceptor.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhangkaiqiang
 * @Date 2019/4/16  9:13
 * @Description TODO
 */

/**
 * spring boot静态文件只能映射到static文件夹,不包括子包
 *boot 2.0以后 WebMvcConfigurerAdapter 这个方法已经过时,使用实现WebMvcConfigurer这个接口来代替继承WebMvcConfigurerAdapter类
 *@author zhangkaiqiang
 */

@Configuration
public class StaticConfig implements WebMvcConfigurer {

	@Autowired
	private LoginInterceptor loginInterceptor;

	@Autowired
	private SendMessageInterceptor sendMessageInterceptor;


	@Autowired
	private AuthInterceptor authInterceptor;


	@Autowired
	private TimeInterceptor timeInterceptor;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		// swagger
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		/**
		 * 登录拦截器与不拦截的路径
		 */
		List<String> excludePath=new ArrayList<>();
		excludePath.add("/");
		excludePath.add("/index.html");
		excludePath.add("/login.html");
		excludePath.add("/static/**");
		excludePath.add("/api/v1/user/login");
		excludePath.add("/api/v1/user/message");
		registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(excludePath);

		registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns(excludePath);

		// 短信拦截器,仅拦截短信接口--控制一点时间内短信发送的次数
		registry.addInterceptor(sendMessageInterceptor).addPathPatterns("/api/v1/user/message");

		// 毕业设计、选题、开题报告等的时间拦截器,判断是否在设定的允许时间内
		List<String> timeInterceptorPatterns=new ArrayList<>();
		timeInterceptorPatterns.add("/api/v1/student/title/*");
		timeInterceptorPatterns.add("/api/v1/student/design");
		timeInterceptorPatterns.add("/api/v1/student/openAndPaper");
		registry.addInterceptor(timeInterceptor).addPathPatterns(timeInterceptorPatterns);
	}
}
